rm(list=ls())

######################################
##### Descriptives, Figures 1-3 ######
######################################

library(readstata13)
library(ggplot2)

election <- read.dta13("IIreadyforreplication.dta")

#Figure 1 
violence <- ggplot(election, aes(x = postviolence)) + xlab("Violent Events") + 
  geom_histogram()

violence

#Figure 2
nonviolence <- ggplot(election, aes(x = postnonviolence)) + xlab("Nonviolent Events") + 
  geom_histogram()

nonviolence

#Figure 3

#Kenya 
events <- read.csv("MELTT_ALL_VACincluded.csv")

events$date<-as.Date(events$date, "%Y-%m-%d")
events <- events[ which(events$country=="Kenya"), ]
events <- events[ which(events$date>"2007-09-27" & events$date<"2008-03-27"), ]

events$week <- as.Date(cut(events$date,
                        breaks = "week",
                        start.on.monday = FALSE)) # changes weekly break point to S

events$nonviolent <- 0
events$nonviolent[events$type==0] <- 1

ggnv <- ggplot(data = events,
               aes(week, nonviolent)) +
  stat_summary(fun.y = sum, # adds up all observations for the week
               geom = "bar") + # or "line"
  scale_x_date(date_breaks = "1 week")

ggnv <- ggnv + theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggnv <- ggnv + scale_y_continuous(limits=c(0, 80))
ggnv <- ggnv + labs(y = "Number of Nonviolent Events")
ggnv <- ggnv + labs(x = "Week")
plot(ggnv)

events$violent <- 0
events$violent[events$type==1] <- 1

Violent 
ggv <- ggplot(data = events,
              aes(week, violent)) +
  stat_summary(fun.y = sum, # adds up all observations for the week
               geom = "bar") + # or "line"
  scale_x_date(date_breaks = "1 week", labels=NULL)

ggv<- ggv + labs(title = "Kenya 2007") + theme(plot.title = element_text(hjust=0.5))
ggv <- ggv + theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggv <- ggv + scale_y_continuous(limits=c(0, 80))
ggv <- ggv + labs(y = "Number of Violent Events")
ggv <- ggv + labs(x = " ")
plot(ggv)

#Zimbabwe 2002
events <- read.csv("MELTT_ALL_VACincluded.csv")

events$date<-as.Date(events$date, "%Y-%m-%d")
events <- events[ which(events$country=="Zimbabwe"), ]
events <- events[ which(events$date>"2001-12-09" & events$date<"2002-06-09"), ]

events$week <- as.Date(cut(events$date,
                           breaks = "week",
                           start.on.monday = FALSE)) # changes weekly break point to S

events$nonviolent <- 0
events$nonviolent[events$type==0] <- 1

#Nonviolent
ggnv <- ggplot(data = events,
               aes(week, nonviolent)) +
  stat_summary(fun.y = sum, # adds up all observations for the week
               geom = "bar") + # or "line"
  scale_x_date(date_breaks = "1 week")

ggnv <- ggnv + theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggnv <- ggnv + scale_y_continuous(limits=c(0, 80))
ggnv <- ggnv + labs(y = "Number of Nonviolent Events")
ggnv <- ggnv + labs(x = "Week")
plot(ggnv)

events$violent <- 0
events$violent[events$type==1] <- 1

#Violent 
ggv <- ggplot(data = events,
              aes(week, violent)) +
  stat_summary(fun.y = sum, # adds up all observations for the week
               geom = "bar") + # or "line"
  scale_x_date(date_breaks = "1 week", labels=NULL)

ggv<- ggv + labs(title = "Zimbabwe 2002") + theme(plot.title = element_text(hjust=0.5))
ggv <- ggv + theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggv <- ggv + scale_y_continuous(limits=c(0, 80))
ggv <- ggv + labs(y = "Number of Violent Events")
ggv <- ggv + labs(x = " ")
plot(ggv)


#South Africa 2009
events <- read.csv("MELTT_ALL_VACincluded.csv")

events$date<-as.Date(events$date, "%Y-%m-%d")
events <- events[ which(events$country=="South Africa"), ]
events <- events[ which(events$date>"2009-01-22" & events$date<"2009-07-22"), ]

events$week <- as.Date(cut(events$date,
                           breaks = "week",
                           start.on.monday = FALSE)) # changes weekly break point to S

events$nonviolent <- 0
events$nonviolent[events$type==0] <- 1

#Nonviolent
ggnv <- ggplot(data = events,
               aes(week, nonviolent)) +
  stat_summary(fun.y = sum, # adds up all observations for the week
               geom = "bar") + # or "line"
  scale_x_date(date_breaks = "1 week")

ggnv <- ggnv + theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggnv <- ggnv + scale_y_continuous(limits=c(0, 30))
ggnv <- ggnv + labs(y = "Number of Nonviolent Events")
ggnv <- ggnv + labs(x = "Week")
plot(ggnv)

events$violent <- 0
events$violent[events$type==1] <- 1

#Violent 
ggv <- ggplot(data = events,
              aes(week, violent)) +
  stat_summary(fun.y = sum, # adds up all observations for the week
               geom = "bar") + # or "line"
  scale_x_date(date_breaks = "1 week", labels=NULL)

ggv<- ggv + labs(title = "South Africa 2009") + theme(plot.title = element_text(hjust=0.5))
ggv <- ggv + theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggv <- ggv + scale_y_continuous(limits=c(0, 30))
ggv <- ggv + labs(y = "Number of Violent Events")
ggv <- ggv + labs(x = " ")
plot(ggv)

#Niger
events <- read.csv("MELTT_ALL_VACincluded.csv")

events$date<-as.Date(events$date, "%Y-%m-%d")
events <- events[ which(events$country=="Niger"), ]
events <- events[ which(events$date>"2009-07-20" & events$date<"2010-01-20"), ]

events$week <- as.Date(cut(events$date,
                           breaks = "week",
                           start.on.monday = FALSE)) # changes weekly break point to S

events$nonviolent <- 0
events$nonviolent[events$type==0] <- 1

#Nonviolent
ggnv <- ggplot(data = events,
               aes(week, nonviolent)) +
  stat_summary(fun.y = sum, # adds up all observations for the week
               geom = "bar") + # or "line"
  scale_x_date(date_breaks = "1 week", labels=NULL)

ggnv <- ggnv + theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggnv <- ggnv + scale_y_continuous(limits=c(0, 30))
ggnv <- ggnv + labs(y = "Number of Nonviolent Events")
ggnv <- ggnv + labs(x = "")
plot(ggnv)

events$violent <- 0
events$violent[events$type==1] <- 1

#Violent 
ggv <- ggplot(data = events,
              aes(week, violent)) +
  stat_summary(fun.y = sum, # adds up all observations for the week
               geom = "bar") + # or "line"
  scale_x_date(date_breaks = "1 week", labels=NULL)

ggv<- ggv + labs(title = "Niger 2009") + theme(plot.title = element_text(hjust=0.5))
ggv <- ggv + theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggv <- ggv + scale_y_continuous(limits=c(0, 30))
ggv <- ggv + labs(y = "Number of Violent Events")
ggv <- ggv + labs(x = " ")
plot(ggv)



######################################
### Prediction curves, Figures 4-7 ###
######################################


curve <- read.csv("Predictions_usmilitary.csv")
library(foreign)
library(dplyr)
library(MASS)
library(AER)
library(stargazer)

#Figure 4
ggplot(curve, aes(y=mean, x=aid)) + geom_line(size=1, colour="gray10") +
  geom_ribbon(aes(ymax=upperbound, ymin=lowerbound), alpha=.25) +
  labs(y="Number of Violent Events", x="U.S.Military Aid (Logged)") + theme_minimal()
ggsave("USMilitaryAid.png", height=4.5, width=7.5, unit="in")

#Figure 5
curve <- read.csv("Predictions_frenchhistory.csv")
ggplot(curve, aes(y=mean, x=french)) + geom_line(size=1, colour="gray10") +
  scale_x_continuous(breaks = seq(0, 1, by = 1)) + 
  geom_ribbon(aes(ymax=upperbound, ymin=lowerbound), alpha=.25) +
  labs(y="Number of Violent Events", x="French Colonial History") + theme_minimal()
ggsave("FrenchColonialHistory.png", height=4.5, width=7.5, unit="in")

#Figure 6
curve <- read.csv("Predictions_hsigosv.csv")
ggplot(curve, aes(y=mean, x=hsigo)) + geom_line(size=1, colour="gray10") +
  scale_x_continuous(limits=) + 
  geom_ribbon(aes(ymax=upperbound, ymin=lowerbound), alpha=.25) +
  labs(y="Number of Violent Events", x="HSIGOs") + theme_minimal()
ggsave("HSIGOV.png", height=4.5, width=7.5, unit="in")

#Figure 7
curve <- read.csv("Predictions_hsigosnv.csv")
ggplot(curve, aes(y=mean, x=hsigo)) + geom_line(size=1, colour="gray10") +
  geom_ribbon(aes(ymax=upperbound, ymin=lowerbound), alpha=.25) +
  labs(y="Number of Nonviolent Events", x="HSIGOs") + theme_minimal()
ggsave("HSIGONV.png", height=4.5, width=7.5, unit="in")





